package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {


    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 根据专辑ID得到渲染专辑详情页面专辑相关信息
     *
     * @param albumId
     * @return {announcer：{主播信息},albumInfo:{专辑信息},albumStatVo:{统计},baseCategoryView:{分类信息}}
     */
    @Override
    public Map<String, Object> albumInfoData(Long albumId) {

        //判断用户访问专辑ID是否存在于布隆过滤器
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        boolean exists = bloomFilter.contains(albumId);
        if(!exists){
            throw new GuiguException(404, "访问专辑不存在！");
        }
        //当采用异步多线程优化后，不能再使用hashMap，采用线程安全集合类
        Map<String, Object> map = new ConcurrentHashMap<>();
        //1.远程调用专辑服务获取专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            Assert.notNull(albumInfo, "专辑{}不存在", albumId);
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolExecutor);

        //2.远程调用专辑服务获取分类
        CompletableFuture<Void> baseCategoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            BaseCategoryView baseCategoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
            Assert.notNull(baseCategoryView, "分类：{}不存在", albumInfo.getCategory3Id());
            map.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor);

        //3.远程调用专辑服务获取统计信息
        CompletableFuture<Void> albumStatVoCompletableFuture = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
            Assert.notNull(albumStatVo, "专辑{}统计信息为空", albumId);
            map.put("albumStatVo", albumStatVo);
        }, threadPoolExecutor);

        //4.远程调用用户服务获取主播信息
        CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
            Assert.notNull(userInfoVo, "用户{}信息为空", albumInfo.getUserId());
            map.put("announcer", userInfoVo);
        }, threadPoolExecutor);

        //5.组合异步任务
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                albumStatVoCompletableFuture,
                baseCategoryViewCompletableFuture,
                announcerCompletableFuture
        ).join();

        return map;
    }
}
